
/*
 Kaswy webcam test software.
 Compil command :  gcc webcam.c -o webcam -I"/usr/include/opencv" -lcv -lhighgui
 Press esc to quit
*/

#include <stdio.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <sys/time.h>


int main(int argc, char **argv) {

	CvCapture *capture = 0;
	IplImage *frame = 0;
	int key = 0;
	int fps = 0;
	struct timeval tempo1, tempo2;
	capture = cvCaptureFromCAM(CV_CAP_ANY);

	if (!capture) {
		fprintf (stderr, "Cam error\n");
		return(-1);
 	}

 	cvNamedWindow("Webcam", CV_WINDOW_AUTOSIZE);
 	gettimeofday(&tempo1, NULL);
  	while(1) {
 		frame = cvQueryFrame(capture);
 		if (frame<0) {
 			printf("break\n");
			break;
 		}

  		//cvFlip( frame, frame, 1);
 		cvShowImage("Webcam", frame);
		if ((key = cvWaitKey(30)) > 0) {
			break;
		}

		fps++;
		gettimeofday(&tempo2, NULL);
		if (tempo2.tv_sec - tempo1.tv_sec == 1) {
		    printf("fps= %d\n",fps);
		    fps=0;
		    tempo1=tempo2;
		}
 	}

	cvDestroyWindow("Webcam");
	cvReleaseCapture(&capture);
	return(0);
}
